﻿using System;
using System.Web.Mvc;
using WeBlog.Interfaces;
using WeBlog.Models;
using WeBlog.Repositories;
using WeBlog.Resources;

namespace WeBlog.Controllers
{
    public class PageController : ApplicationController
    {
        public PageController(IUnitOfWork unitOfWork )
            : base( unitOfWork )
        {       
        }

        [WeBlogAuthorize(Role.Admin), Theme("Admin")]
        public ActionResult Manage( GridSettings settings )
        {
            var model = new PageListViewModel(settings, UnitOfWork);
            TryUpdateModel(model);
            return View(model);
		}
                
        [HttpGet, WeBlogAuthorize(Role.Admin), Theme("Admin")]
        public ActionResult Create()
        {
            var model = new PageFormViewModel(UnitOfWork, CurrentUser);
            return View(model);
        }

        public ActionResult Details(string slug)
        {
            var model = new PageFormViewModel(UnitOfWork, slug, CurrentUser);
            return View(model);
        }

        [HttpPost, ValidateInput(false), WeBlogAuthorize(Role.Admin), Theme("Admin")]
        public ActionResult Create(Page page)
        {
            var repository = new PageRepository(UnitOfWork);
            page.LastModified = DateTime.Now;                

            if (String.IsNullOrWhiteSpace(page.Slug))
                page.Slug = Utils.RemoveIllegalCharacters(page.Title);

            if (ModelState.IsValid)
            {
                repository.Add(page);
                UnitOfWork.Commit();
                TempData.SetStatusMessage(Strings.Page_SavedSuccessfully);
                return RedirectToAction("Edit", new { id = page.PageId });
            }

            var model = new PageFormViewModel(UnitOfWork, CurrentUser) { Page = page };
            return View(model);
        }

        [WeBlogAuthorize(Role.Admin), Theme("Admin")]
        public ActionResult Edit(int id)
        {
            var model = new PageFormViewModel(UnitOfWork, id, CurrentUser);
            return View(model);
        }

        [HttpPost, WeBlogAuthorize(Role.Admin), ValidateInput(false), Theme("Admin")]
        public ActionResult Edit(Page page)
        {
            UnitOfWork.Attach(page);
            page.LastModified = DateTime.Now;                
            if (ModelState.IsValid)
            {
                UnitOfWork.Commit();                    
                TempData.SetStatusMessage(Strings.Page_SavedSuccessfully);
            }

            var model = new PageFormViewModel(UnitOfWork, CurrentUser) { Page = page };
            return View(model);
        }

        [HttpPost, WeBlogAuthorize(Role.Admin)]
        public JsonResult Delete(int id)
        {
            try
            {
                var repository = new PageRepository(UnitOfWork);
                var page = repository.Find(id);
                if (page != null)
                {
                    repository.Remove(page);
                    UnitOfWork.Commit();
                }
                return Json(new JsonResponse { Success = true, Message = String.Empty });
            }
            catch (Exception ex)
            {
                return Json(new JsonResponse { Message = ex.Message, Success = false });
            }
        }

    }
}
